package com.vooda.service.impl;

import com.vooda.constant.ErrorCode;
import com.vooda.constant.RedisKeyPrefix;
import com.vooda.constant.SmsTemplateCode;
import com.vooda.dto.vo.BaseResult;
import com.vooda.service.VerifyCodeService;
import com.vooda.util.RandomUtil;
import com.vooda.util.SmsUtil;
import com.vooda.util.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * 验证码Service实现类
 *
 * @author charlee
 * @date 2017/12/8
 */
@Slf4j
@Service
public class VerifyCodeServiceImpl implements VerifyCodeService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private MailService mailService;

    @Override
    public BaseResult sendVerifyCode(String account, Integer codeType) {
        String code = RandomUtil.generate(6);
        if (ValidationUtil.isMobile(account)) {
            if (ObjectUtils.isEmpty(codeType)) {
                codeType = SmsTemplateCode.COMMON.getCodeType();
            }

            for (SmsTemplateCode smsTemplateCode : SmsTemplateCode.values()) {
                if (codeType == smsTemplateCode.getCodeType()) {
                    redisTemplate.opsForValue().set(RedisKeyPrefix.SMS_VERIFY_CODE + account, code, 15, TimeUnit.MINUTES);
                    SmsUtil.sendSms(account, code, smsTemplateCode.getTemplateCode());
                    return BaseResult.getDefaultSuccessResult();
                }
            }
            return BaseResult.builder().code(ErrorCode.PARAM_ERROR.getCode()).msg("codeType错误").build();
        }

        if (ValidationUtil.isEmail(account)) {
            redisTemplate.opsForValue().set(RedisKeyPrefix.EMAIL_VERIFY_CODE + account, code, 15, TimeUnit.MINUTES);
            mailService.sendEmailVerifyCode(account, code);
            return BaseResult.getDefaultSuccessResult();
        }
        return BaseResult.builder().code(ErrorCode.PARAM_ERROR.getCode()).msg("请输入正确的手机号或邮箱账号").build();
    }

    @Override
    public Boolean checkVerifyCode(String account, String verifyCode) {
        if (ValidationUtil.isMobile(account)) {
            if (verifyCode.equals(redisTemplate.opsForValue().get(RedisKeyPrefix.SMS_VERIFY_CODE + account))) {
                redisTemplate.delete(RedisKeyPrefix.SMS_VERIFY_CODE + account);
                return true;
            }
            return false;
        }

        if (ValidationUtil.isEmail(account)) {
            if (verifyCode.equals(redisTemplate.opsForValue().get(RedisKeyPrefix.EMAIL_VERIFY_CODE + account))) {
                redisTemplate.delete(RedisKeyPrefix.EMAIL_VERIFY_CODE + account);
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public void saveImageVerifyCode(String randomCode, String verifyCode) {
        redisTemplate.opsForValue().set(RedisKeyPrefix.IMAGE_VERIFY_CODE + randomCode, verifyCode, 180, TimeUnit.SECONDS);
    }

    @Override
    public Boolean checkImageVerifyCode(String randomCode, String verifyCode) {
        String code = (String) redisTemplate.opsForValue().get(RedisKeyPrefix.IMAGE_VERIFY_CODE + randomCode);
        return StringUtils.hasText(code) && code.equals(verifyCode);
    }
}
